//
//  BookingsHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/20/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "BookingsHandler.h"
#import "BookingsProtocol.h"
#import "Booking.h"
#import "Teacher.h"
#import "Utils.h"
#import "BookingsProtocol.h"

#define DATE_FORMAT @"yyyy-MM-dd HH:mm:ss"

@implementation BookingsHandler


- (void)handleSuccess:(WSResponse *)response {
    NSArray *bookingsJSONArray = [response.data objectFromJSONData];
    NSMutableArray *bookingsArray = nil;
    if ([bookingsJSONArray count] > 0)
        bookingsArray = [NSMutableArray new];
    
    for (NSDictionary *bookingJSON in bookingsJSONArray) {
        Booking *booking = [Booking new];
        
        // Booking Id
        booking.bookingId = [[bookingJSON getJSONObjectForKey:BOOKING_ID_TAG] integerValue];
        
        // Booking Start Date
        booking.startDate = [Utils dateFromString:[bookingJSON getJSONObjectForKey:BOOKING_START_DATE_TAG] withFormat:DATE_FORMAT];
        
        // Booking End date
        booking.endDate = [Utils dateFromString:[bookingJSON getJSONObjectForKey:BOOKING_END_DATE_TAG] withFormat:DATE_FORMAT];
        
        // Booking teacher
        Teacher *teacher = [Teacher new];
        
        NSDictionary *teacherJSON = [bookingJSON getJSONObjectForKey:BOOKING_TEACHER_TAG];
        teacher.teacherId = [[teacherJSON getJSONObjectForKey:TEACHER_ID_TAG] integerValue];
        teacher.firstName = [teacherJSON getJSONObjectForKey:TEACHER_FIRST_NAME_TAG];
        teacher.lastName = [teacherJSON getJSONObjectForKey:TEACHER_LAST_NAME_TAG];
        teacher.position = [teacherJSON getJSONObjectForKey:TEACHER_POSITION_TAG];
        
        booking.teacher = teacher;
        // Booking course
        Course *course = [Course new];
        NSDictionary *courseJSON = [bookingJSON getJSONObjectForKey:BOOKING_COURSE_TAG];
        course.courseCode = [courseJSON getJSONObjectForKey:COURSE_CODE_TAG];
        course.courseName = [courseJSON getJSONObjectForKey:COURSE_NAME_TAG];
        course.faculty = [Faculty new];
        course.faculty.facultyId = [[courseJSON getJSONObjectForKey:FACULTY_ID_TAG] integerValue];
        course.selfUrl = [courseJSON getJSONObjectForKey:SELF_URL_TAG];
        booking.course = course;
        
        // Booking Room
        Room *room = [Room new];
        NSDictionary *roomJSON = [bookingJSON getJSONObjectForKey:BOOKING_ROOM_TAG];
        room.roomId = [[roomJSON getJSONObjectForKey:ROOM_ID_TAG] integerValue];
        room.name = [roomJSON getJSONObjectForKey:ROOM_NAME_TAG];
        room.selfUrl = [roomJSON getJSONObjectForKey:SELF_URL_TAG];
        
        booking.room = room;
        
        // Building
        Building *building = [Building new];
        building.buildingId = [[roomJSON getJSONObjectForKey:BUILDING_ID_TAG] integerValue];
        building.name = [roomJSON getJSONObjectForKey:BUILDING_NAME_TAG];
        booking.room.building = building;
        
        // Type of class
        TypeOfClass *typeOfClass = [TypeOfClass new];
        NSDictionary *typeOfClassJSON = [bookingJSON getJSONObjectForKey:BOOKING_TYPE_OF_CLASS_TAG];
        typeOfClass.typeOfClassId = [[typeOfClassJSON getJSONObjectForKey:TYPE_OF_CLASS_ID_TAG] integerValue];
        typeOfClass.name = [typeOfClassJSON getJSONObjectForKey:TYPE_OF_CLASS_NAME_TAG];
        
        booking.typeOfClass = typeOfClass;
        
        // Study groups
        NSArray *studyGroupsJSON = [bookingJSON getJSONObjectForKey:BOOKING_STUDY_GROUPS_TAG];
        NSMutableArray *studyGroups = [NSMutableArray new];
        
        for (NSDictionary *studyGroupDict in studyGroupsJSON) {
            StudyGroup *studyGroup = [StudyGroup new];
            studyGroup.studyGroupId = [[studyGroupDict getJSONObjectForKey:STUDY_GROUP_ID_TAG] integerValue];
            studyGroup.name = [studyGroupDict getJSONObjectForKey:STUDY_GROUP_NAME_TAG];
            
            [studyGroups addObject:studyGroup];
        }
        
        booking.studyGroups = studyGroups;
        
        // Self URL
        booking.selfURL = [bookingJSON getJSONObjectForKey:SELF_URL_TAG];
        
        [bookingsArray addObject:booking];
        
    }

    [self.delegate didReceiveBookingsWithSuccess:bookingsArray forTicket:self.ticket];

}

- (void)handleError:(WSError *)error {
    [self.delegate didReceiveBookingsWithFailure:error forTicket:self.ticket];
}
@end
